<script setup>
import { onMounted, reactive, ref } from "vue";
import { test, updateEntity } from "./api/EntityApi.js";

let props = defineProps({
  dialogVisible: {
    type: Boolean,
    default: false
  },
  form: {
    type: Object,
    default: {}
  },
});

// 定义更新函数
const emit = defineEmits(['update:dialogVisible', 'refresh']);

let formRef = ref(null);
let form = reactive(props.form);
const tableData = ref([]);

// 编辑
const handleUpdate = () => {
  updateEntity(form.id, form).then(() => {
    emit('update:dialogVisible', false);
    emit('refresh');
  });
};

// 测试
const handleTest = () => {
  test(form.id, form).then((data) => {
    if (data) {
      tableData.value = data;
    }
  });
};

const cancel = () => {
  tableData.value = [];
  emit('update:dialogVisible', false);
  emit('refresh');
};

onMounted(() => {
});
</script>

<template>
  <!-- 弹窗-修改 -->
  <el-dialog v-model="props.dialogVisible" title="详情" width="60%" @close="cancel" custom-class="custom-dialog">
    <el-form :model="form" ref="formRef" label-width="120px" style="padding-right: 30px;">
      <el-form-item
          key="executeStr"
          label="执行语句"
          prop="executeStr"
      >
        <el-input
            v-model="form.executeStr"
            autocomplete="off"
            type="textarea"
            :autosize="{ minRows: 4 }"
        />
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%; margin-top: 20px;" fit>
      <el-table-column
          v-for="(value, key) in tableData[0]"
          :key="key"
          :label="key"
          :prop="key"
          show-overflow-tooltip
      >
      </el-table-column>
    </el-table>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="handleTest">刷新</el-button>
        <el-button type="success" @click="handleUpdate">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped>
.custom-dialog {
  padding: 20px;
}

.el-form {
  margin-bottom: 20px;
}

.el-table {
  margin-top: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}

.el-button {
  margin-left: 10px;
}

.el-form-item__label {
  font-weight: bold;
}

.el-textarea__inner {
  font-family: 'Courier New', Courier, monospace;
}

.el-table__body-wrapper {
  max-height: 300px;
  overflow-y: auto;
}
</style>
